﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WebCrawling.Data.Extracting
{
    public class Output
    {
        #region Field

        private static Dictionary<Uri, Output> mDicOutputs = new Dictionary<Uri, Output>();
        private Dictionary<string, string> mDicFields = new Dictionary<string, string>();
        private static Output mOutput;
        #endregion

        #region Properties
        public static Output Instance
        {
            get
            {
                if (mOutput == null)
                {
                    mOutput = new Output();
                }
                return mOutput;
            }
        }

        public static void Reset()
        {
            mDicOutputs.Clear();
            mOutput = null;
        }

        public Uri Url
        {
            get;
            set;
        }

        public int Links
        {
            get;
            set;
        }

        public string Rank
        {
            get;
            set;
        }

        public Dictionary<string, string> Fields
        {
            get { return mDicFields; }
        }

        #endregion

        #region Method

        public Dictionary<Uri, Output> DicOutputs()
        {
              return mDicOutputs;    
        }

        public bool Contain(Output Output)
        {
            bool blResult = false;

            foreach (Output s in mDicOutputs.Values)
            {
                if (Output.Url == s.Url)
                {
                    blResult = true;
                    break;
                }
            }

            return blResult;
        }
        public Output Find(string Name)
        {
            Output Output = null;

            foreach (Uri key in mDicOutputs.Keys)
            {
                Output = mDicOutputs[key];

                if (Output.Url == Url)
                {
                    return Output;
                }
            }
            return null;
        }

        public void RemoveOutput(string Name)
        {
            Output Output = null;

            foreach (Uri key in mDicOutputs.Keys)
            {
                Output = mDicOutputs[key];

                if (Output.Url == Url)
                {
                    mDicOutputs.Remove(key);
                    break;
                }
            }
        }

        public void AddOutput(Output Output)
        {
            if (!Contain(Output))
            {
                mDicOutputs.Add(Output.Url, Output);
            }
        }

        public void Clear()
        {
            mDicFields.Clear();
        }
        #endregion

    }
}
